package cs342.uwosh.edu.QuizApp.storage;

import java.util.ArrayList;



public class Quiz 
{
	public static final String TAG = "quiz";
	public static final String DESCR = "descr";
	
	public static final String KEY = "key";
	public static final String NONKEY = "nonkey";
	
	public static final String TF = "questionTF";
	public static final String MC = "questionMC";
	public static final String FB = "questionFB";
	
	// Name
	private String name;
	
	// Course
	private String course;
	
	//	Description of Quiz
	private String description;
	
	//	List of Questions
	private ArrayList<Question> questions;
	
	//	Current Question user is on
	private int current;
	
	//  Farthest Question user has has seen
	private int farthest;
	
	
	public Quiz(String name, String course, String description) {
		this.name = name;
		this.setCourse(course);
		this.setDescription(description);
		this.questions = new ArrayList<Question>();
	}
	
	/** 
	 * AddQuestion - adds question to the list of questions
	 * 
	 * 	@param q - question to be added
	 */
	
	public void addQuestion(Question q) {
		questions.add(q);
	}
	
	/** 
	 * Start - initializes the quiz by setting the current question to
	 * 			the first question in the list.
	 */
	
	public void start() {
		farthest = 0;
		current = 0;
	}
	
	
	public Question getCurrentQuestion() {
		return questions.get(current);
	}
	
	/** 
	 * GetNextQuestion - retrieves the next question (current) in the list of questions
	 */
	
	public void nextQuestion() {
		if(isOnLastQuestion()) {
			System.err.println("Quiz - attempted to get " +
					"next question at the end of the quiz.");
		} else {
			current++;
			if(current > farthest) farthest = current;
		}
	}
	
	/**
	 * PreviousQuestion updates the current question to the previous question
	 */
	
	public void previousQuestion() {
		if(!isOnFirstQuestion()) {
			System.err.println("Quiz - attempted to get " +
					"previous question when there are none.");
		} else {
			current--;
		}
	}
	
	/**
	 * isFirstQuestion - determines if the current question is
	 * 					the first question
	 * @return boolean
	 */
	
	public boolean isOnFirstQuestion() {
		return current == 1 || current == 0;
	}
	
	/**
	 * isLastQuesiton - determines if the current question is the
	 * 					last question
	 * @return boolean
	 */
	
	public boolean isOnLastQuestion() {
		return current == questions.size()-1;
	}
	
	/**
	 * GetQuestionNumber 
	 * @return the current question number
	 */
	
	public int getQuestionNumber() {
		return current + 1;
	}
	
	/**
	 * getQuestionCount
	 * @return 
	 */
	
	public int getQuestionCount() {
		return questions.size();
	}
	
	public ArrayList<Question> getQuestions() {
		return questions;
	}

	public void setQuestions(ArrayList<Question> questions) {
		this.questions = questions;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getCourse() {
		return course;
	}

	public void setCourse(String course) {
		this.course = course;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String toString() {
		StringBuffer str = new StringBuffer();
		for(Question q : questions) str.append(q.toString());
		return str.toString();
	}

	public int getScore() {
		int num = 0;
		for(int i = 0; i < farthest; i++ ) {
			if(questions.get(i).isCorrect()) {
				num++;
			}
		}
		return num;
	}
	

	
}
